package com.zhm.demo.plugins;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ReferenceUtil;
import cn.hutool.core.util.ReflectUtil;
import com.zhm.demopluginapi.PluginApiTest;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.pf4j.PluginWrapper;

import java.nio.file.Paths;
import java.util.List;

import static java.lang.Class.forName;

@Slf4j
public class PluginTest {

    public static void main(String[] args) {
        PluginManager pluginManager = new JarPluginManager();

        String pluginId = "welcome-plugin";
        PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId);
        if (ObjUtil.isNull(pluginWrapper)) {
            System.out.println("不存在" + pluginId);
        }

        // 加载插件
        pluginId = pluginManager.loadPlugin(Paths.get("D:\\jar\\demo-plugin-0.0.0-SNAPSHOT.jar"));


        // 启动插件
        pluginManager.startPlugins();

        log.info("Plugindirectory: {}", System.getProperty("pf4j.pluginsDir", "plugins"));

        // 获取所有Greeting扩展点的实例
        List<PluginApiTest> extensions = pluginManager.getExtensions(PluginApiTest.class, pluginId);

        List<Class<?>> extensionClasses = pluginManager.getExtensionClasses(pluginId);

        List extensions1 = pluginManager.getExtensions(pluginId);

        extensions1.forEach(ec->{
            try {
                if(ec.getClass().getName().equals("com.zhm.demoplugin.PluginApiTestImpl2") ){
                    Object a = ReflectUtil.invoke(ec, "a", new Object[0]);
                    System.out.println(a);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        });

        // 执行插件的功能
        for (PluginApiTest pluginApiTest : extensions) {
            System.out.println(pluginApiTest.a());
        }

        pluginManager.stopPlugin(pluginId);
        pluginManager.unloadPlugin(pluginId);

        System.out.println(extensions.size());

        for (PluginApiTest pluginApiTest : extensions) {
            System.out.println(pluginApiTest.a());
        }

        // 加载插件
        pluginId = pluginManager.loadPlugin(Paths.get("D:\\jar\\demo-plugin-0.0.1-SNAPSHOT.jar"));


        // 启动插件
        pluginManager.startPlugins();


        // 获取所有Greeting扩展点的实例
        List<PluginApiTest> extensions2 = pluginManager.getExtensions(PluginApiTest.class, pluginId);
        List<PluginApiTest> extensions3 = pluginManager.getExtensions(PluginApiTest.class, pluginId);


        // 执行插件的功能
        for (PluginApiTest pluginApiTest : extensions2) {
            System.out.println(pluginApiTest.toString());
            System.out.println(pluginApiTest.a());
        }
        for (PluginApiTest pluginApiTest : extensions3) {
            System.out.println(pluginApiTest.toString());
            System.out.println(pluginApiTest.a());
        }
        // print extensions for each started plugin
//        List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
//        for (PluginWrapper plugin : startedPlugins) {
//            String pluginId = plugin.getDescriptor().getPluginId();
//            log.info(String.format("Extensions added by plugin '%s':", pluginId));
//            // extensionClassNames = pluginManager.getExtensionClassNames(pluginId);
//            // for (String extension : extensionClassNames) {
//            //     logger.info("   " + extension);
//            // }
//        }


        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            pluginManager.stopPlugins();
            pluginManager.unloadPlugins();
        }));


    }
}
